package br.com.sistema.smallAgro.cliente;

import java.util.Arrays;

import org.apache.wicket.Component;
import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.ajax.markup.html.form.AjaxButton;
import org.apache.wicket.feedback.ExactLevelFeedbackMessageFilter;
import org.apache.wicket.feedback.FeedbackMessage;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.RadioChoice;
import org.apache.wicket.markup.html.form.TextField;
import org.apache.wicket.markup.html.link.Link;
import org.apache.wicket.markup.html.panel.FeedbackPanel;
import org.apache.wicket.model.CompoundPropertyModel;
import org.apache.wicket.model.PropertyModel;

import br.com.sistema.smallAgro.enums.IRenderStatus;
import br.com.sistema.smallAgro.enums.SimNao;
import br.com.sistema.smallAgro.modelo.Cliente;
import br.com.sistema.smallAgro.util.Constantes;

public abstract class CadastrarClienteForm extends Form<Cliente> {
	private static final long serialVersionUID = 8771981958772406834L;
	private RadioChoice<SimNao> status;
	private Component feedback;
	private Component feedbackError;

	public CadastrarClienteForm(String id, Cliente cliente) {
		super(id, new CompoundPropertyModel<Cliente>(cliente));
		setOutputMarkupId(true);
		addOrReplace(feedbackError = new FeedbackPanel("msgErro",
				new ExactLevelFeedbackMessageFilter(FeedbackMessage.ERROR))
				.setOutputMarkupId(true));

		addOrReplace(feedback = new FeedbackPanel("msgSucesso",
				new ExactLevelFeedbackMessageFilter(FeedbackMessage.SUCCESS))
				.setOutputMarkupId(true));

		componentes();
	}

	private void componentes() {
		add(new TextField<String>("nome"));

		add(new TextField<String>("endereco"));

		add(new TextField<String>("telefone"));
		add(new TextField<String>("contato1"));
		add(new TextField<String>("telefone1"));
		add(new TextField<String>("contato2"));
		add(new TextField<String>("telefone2"));

		status = new RadioChoice<SimNao>("status", new PropertyModel<SimNao>(
				getModelObject(), "status"), Arrays.asList(SimNao.values()),
				new IRenderStatus());
		add(status);

		add(new AjaxButton("salvar") {
			private static final long serialVersionUID = -381365317368827695L;

			@Override
			protected void onSubmit(AjaxRequestTarget target, Form<?> form) {
				isValido();

				if (!getSession().getFeedbackMessages().isEmpty()) {
					target.add(feedbackError);
				} else if (buscaClienteValidacao((Cliente) form
						.getModelObject())) {
					feedbackError.error("Já existe login cadastrado!");
				} else {
					Cliente cliente = (Cliente) form.getModelObject();
					cliente.setStatus(SimNao.S);
					salvarDados(cliente);
					if (cliente.getCodigo() == null) {
						feedback.success(Constantes.DADOS_SALVOS);
					} else {
						feedback.success(Constantes.DADOS_ALTERADOS);
					}
				}
				target.add(feedback);
				target.add(feedbackError);
				target.appendJavaScript(Constantes.SCRIPT_SUBIR);
			}
		});

		add(new Link<Void>("voltar") {
			private static final long serialVersionUID = -283402666944796774L;

			@Override
			public void onClick() {
				setResponsePage(new ConsultarClientePage());
			}
		});
	}

	private void isValido() {
		if ((getModelObject().getNome() == null)
				|| (getModelObject().getStatus() == null)
				|| (getModelObject().getEndereco() == null)
				|| (getModelObject().getTelefone() == null)) {
			feedbackError.error(Constantes.DADO_OBRIGATORIO);
		}
	}

	protected abstract void salvarDados(Cliente cliente);

	protected abstract boolean buscaClienteValidacao(Cliente cliente);
}
